#include "Rp2Main.h"
#include "Rp2Object.h"

using namespace Rp2;

Main::InitializerArray* Main::ms_pkInitializers = 0;
Main::TerminatorArray* Main::ms_pkTerminators = 0;

Catalog<Image>* Main::ms_pkImageCatalog = 0;
Catalog<Texture>* Main::ms_pkTextureCatalog = 0;
Catalog<VertexProgram>* Main::ms_pkVertexProgramCatalog = 0;
Catalog<PixelProgram>* Main::ms_pkPixelProgramCatalog = 0;
//---------------------------------------------------------------------------------------------------
Main::Main()
{
}
//---------------------------------------------------------------------------------------------------
Main::~Main()
{
}
//---------------------------------------------------------------------------------------------------
void Main::AddInitializer(Initializer oInitializer)
{
	if (!ms_pkInitializers)
	{
		ms_pkInitializers = new InitializerArray;
	}
	ms_pkInitializers->push_back(oInitializer);
}
//---------------------------------------------------------------------------------------------------
void Main::Initialize()
{
	ms_pkImageCatalog = new Catalog<Image>("Main");
	Catalog<Image>::SetActive(ms_pkImageCatalog);

	ms_pkTextureCatalog = new Catalog<Texture>("Main");
	Catalog<Texture>::SetActive(ms_pkTextureCatalog);
	
	ms_pkVertexProgramCatalog = new Catalog<VertexProgram>("Main");
	Catalog<VertexProgram>::SetActive(ms_pkVertexProgramCatalog);

	ms_pkPixelProgramCatalog = new Catalog<PixelProgram>("Main");
	Catalog<PixelProgram>::SetActive(ms_pkPixelProgramCatalog);

	if (ms_pkInitializers)
	{
		for (int i = 0; i < (int)ms_pkInitializers->size(); i++)
		{
			(*ms_pkInitializers)[i]();
		}
	}

	delete ms_pkInitializers;
	ms_pkInitializers = 0;
}
//---------------------------------------------------------------------------------------------------
void Main::AddTerminator (Terminator oTerminate)
{
    if (!ms_pkTerminators)
    {
        ms_pkTerminators = new TerminatorArray;
    }

    ms_pkTerminators->push_back(oTerminate);
}
//---------------------------------------------------------------------------------------------------
void Main::Terminate()
{
	if (ms_pkTerminators)
	{
		for (int i = 0; i < (int)ms_pkTerminators->size(); i++)
		{
			(*ms_pkTerminators)[i]();
		}
	}
	
	delete ms_pkTerminators;
	ms_pkTerminators = 0;

	if (Catalog<PixelProgram>::GetActive() == ms_pkPixelProgramCatalog)
	{
		Catalog<PixelProgram>::SetActive(0);
	}
	delete ms_pkPixelProgramCatalog;

	if (Catalog<VertexProgram>::GetActive() == ms_pkVertexProgramCatalog)
	{
		Catalog<VertexProgram>::SetActive(0);
	}
	delete ms_pkVertexProgramCatalog;

	if (Catalog<Texture>::GetActive() == ms_pkTextureCatalog)
	{
		Catalog<Texture>::SetActive(0);
	}
	delete ms_pkTextureCatalog;

	if (Catalog<Image>::GetActive() == ms_pkImageCatalog)
	{
		Catalog<Image>::SetActive(0);
	}
	delete ms_pkImageCatalog;
}
//---------------------------------------------------------------------------------------------------